package com.qf.lanzhou.blog.api.controller.notification;

import com.alibaba.fastjson.JSON;
import com.qf.lanzhou.blog.api.controller.BaseServletPost;
import com.qf.lanzhou.blog.api.pojo.entity.Notification;
import com.qf.lanzhou.blog.api.pojo.entity.Page;
import com.qf.lanzhou.blog.api.pojo.vo.BaseRespVO;
import com.qf.lanzhou.blog.api.service.NotificationService;
import com.qf.lanzhou.blog.api.service.impl.NotificationServiceImpl;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 是柠檬甜
 * @ClassName NotificationShowServlet
 * @description 通知列表展示功能
 * @date 2020/12/16 10:15
 * @modifier 是柠檬甜
 * @date 2020-12-16 10:15
 * @Version V1.0
 */
@WebServlet("/notificationShowAll.do")
public class NotificationShowServlet extends BaseServletPost {
    private NotificationService notificationService=new NotificationServiceImpl();
    private BaseRespVO result=new BaseRespVO();
    public static Integer numberPage;
    private Page page = new Page(1);
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
//        resp.setContentType("text/html;charset=utf-8");
//        List<Notification> list = notificationService.showAll();
//        for (Notification notification:list) {
//            notification.setNotificationTimeStr(notification.getNotificationTime());
//        }
//        successResultWrite(list,resp);
        //获取参数
        System.out.println("消息通知服务器连接");
        String sta = req.getParameter("a");
        String stb = req.getParameter("b");
        System.out.println(sta+"     "+stb);
        System.out.println("获取参数");
        Integer a = Integer.decode(sta);
        Integer b = Integer.decode(stb);
        System.out.println("参数转换完成");
        //获取总页数 count
        Integer count = notificationService.getCount();
        //判断是前进还是后退还是按页码查询
        System.out.println(a+"         "+b);
        System.out.println(count);
        page.setTotalCounts(count);
        if (b!= -168){
            //按照页码查询
            if (b<=0 || b> page.getTotalPage()){
                String msg = "不符合搜索标准哦";
                failedResultWrite(msg,resp);
                System.out.println("跳转页码错误");
                return;
            }
            numberPage = b;
            List<Notification> list = notificationService.selectPage(numberPage);
            for (Notification notification:list) {
            notification.setNotificationTimeStr(notification.getNotificationTime());
        }
            successResultWrite(list,resp);
            System.out.println("跳转页码成功");
            return;
        }
        if (a ==0){
            //前进
            if (numberPage==page.getTotalPage()){
                String msg = "现在已经是最后一页了哦";
                failedResultWrite(msg,resp);
                return;
            }
            numberPage++;
            List<Notification> list = notificationService.selectPage(numberPage);
            for (Notification notification:list) {
                notification.setNotificationTimeStr(notification.getNotificationTime());
            }
            successResultWrite(list,resp);
            return;
        }
        if (numberPage == 1){
            String msg = "前面没有内容了哦";
            failedResultWrite(msg,resp);
            return;
        }
        numberPage--;
        List<Notification> list = notificationService.selectPage(numberPage);
        for (Notification notification:list) {
            notification.setNotificationTimeStr(notification.getNotificationTime());
        }
        successResultWrite(list,resp);

        //条件判断
        //返回相应结果
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
